/**
 * Functions.js
 **/

if (!window.requestAnimationFrame) {
	window.requestAnimationFrame = (function() {
		return window.webkitRequestAnimationFrame ||
				window.mozRequestAnimationFrame ||
				window.oRequestAnimationFrame ||
				window.msRequestAnimationFrame ||
				function (callback, elements) {
					window.setTimeout(callback, 1000/40);
				}
	})();
}

// console.log shortcut
function log(message) {
	return console.log(message);
}

// console.warn shortcut
function warn(message) {
	return console.warn(message);
}

// querySelectorAll shortcut
function $(id) {
	return document.getElementById(id);
}

function $s(selector) {
	var qsa = document.querySelectorAll(selector);
	return (qsa.length === 1 ? qsa[0] : qsa);
}

// Print something in the HTML
function debug(message) {
	var debug = document.querySelector('#debug');
	if (!debug) {
		debug = document.createElement('div');
		debug.setAttribute('id', 'debug');
		document.body.appendChild( debug );
	}

	debug.innerText = message;
}
// Check a variable is a number
function isNumber(value) {
    if ((undefined === value) || (null === value)) {
        return false;
    }
    if (typeof value == 'number') {
        return true;
    }
    return !isNaN(value - 0);
}

// Compute a random value between 'a' and 'b'
function rand(a, b) {
	return a + Math.random() * (b - a);
}

// Détermine la distance entre 2 points
function distance(a, b){
	var distance = Math.sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
	return distance;
}

// Detect collision between two objects 'a' and 'b'
// Assuming both 'a' and 'b' have x/y/w/h properties
function collide(a, b) {
	if (a == undefined || b == undefined)
		return false;

	if(typeof a.w === "undefined" || typeof a.h === "undefined" || typeof b.w === "undefined" || typeof b.h === "undefined")
		return false;

	return !(b.x >= a.x + a.w // Trop à droite
			|| b.x + b.w <= a.x // Trop à gauche
			|| b.y >= a.y + a.h // Trop en bas
			|| b.y + b.h <= a.y) // Trop en haut
}

// Pickup a random value into an Array
Array.prototype.pickup = Array.prototype.pickup || function() {
	return this[ Math.floor(Math.random()*this.length) ];
}

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

// Teste si un interval est passé
function interval(time, interval) {
	return (+new Date() - time) / 1000 > interval;
}

/* Graphics */
// Draw rectangle
function drawRect(context,color,x,y,width,height) {
	context.fillStyle = color;
	context.fillRect(x, y, width, height);
}

function lineDistance( point1, point2 ) {
  var xs = 0;
  var ys = 0;
 
  xs = point2.x - point1.x;
  xs = xs * xs;
 
  ys = point2.y - point1.y;
  ys = ys * ys;
 
  return Math.sqrt( xs + ys );
}

function getIMName(src) {
	return src.split('.')[0];
}

// Class manipulation
function hasClass(ele,cls) {
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
function addClass(ele,cls) {
	if (ele.length === undefined)
		ele = [ele];
	
	for (x in ele) {
		if (!isNaN(x)) {
			if (!this.hasClass(ele[x],cls)) ele[x].className += " "+cls;
		}
	}
}
function removeClass(ele,cls) {
	if (ele.length === undefined)
		ele = [ele];
	
	for (x in ele) {
		if (!isNaN(x)) {
			if (hasClass(ele[x],cls)) {
				var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
				ele[x].className=ele[x].className.replace(reg,' ');
			}
		}
	}
}

// Form manipulation
function getSelected(select) {
	return select.options[select.selectedIndex];
}


//Fonction pour faire coller les blocs à la grille de jeu
function getPos(rand, size, borne){
	for(i=0; i<=borne; i += size){
		if(rand < i+size){
			return i;
		}
	}
}

function convertTimeToString(time) {
	var timer = '';
	var minutes = Math.floor(time/60);
	if (minutes < 10) timer = '0' + minutes;
	else timer = minutes;
	timer += ':';
	var seconds = time%60;
	if (seconds < 10) timer += '0' + seconds; 
	else timer += seconds;

	return timer;
}

function renderParticules(particules){
	var iP = 0;

	while(iP < particules.length){
		var p = particules[iP++];
			ctx.fillStyle = p.c;
			ctx.fillRect(p.x, p.y, p.s, p.s);
	}
}

function animParticles(particules, typeAnim){
	var iP = 0;
	while(iP < particules.length){
		var p = particules[iP];
		p.x += p.xs;
		p.y += p.ys;
		p.l --;

		switch(typeAnim){
			case 'gravity' : 
				if(p.l == 0){
					if(p.d){
						game.bonus.showGravity(20);
					}
					particules.splice(iP--, 1);
				}	
			break;
			default: 
				if(p.l == 0){
					particules.splice(iP--, 1);
				}					
			break;
		}
		
		iP++;
	}
}

function clearTable(tableID) {
	var table = document.getElementById(tableID);

    for (var i = table.rows.length-1 ; i > 0; i--) {
    	table.deleteRow(i);
    };
}

function addRow(tableID, data) {
    var table = document.getElementById(tableID);

    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);

    var cell1 = row.insertCell(0);
    cell1.innerHTML = data.name;

    var cell2 = row.insertCell(1);
    cell2.innerHTML = data.score;

    var cell3 = row.insertCell(2);
    cell3.innerHTML = data.time;
}